package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.util.DeviceUtil;

/**
 * Created by chenchangxing on 16/5/25.
 */
public class GvImgAdapter extends RecyclerView.Adapter {

    OnImgDeleteListener mOnImgDeleteListener;
    OnItemClickListener mOnItemClickListener;
    List<File> mFiles;
    Context mContext;

    public GvImgAdapter(List<File> list) {
        DeviceUtil.Log("adfadfsdfsdf");
        this.mFiles = list;
    }

    public GvImgAdapter() {
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mFiles.size()) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        if (viewType == 0) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gv_img, parent, false);
            return new ViewHolder0(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gv_add, parent, false);
            return new ViewHolder1(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        DeviceUtil.Log("adafasfafsf");
        if (getItemViewType(position) == 0) {
            ViewHolder0 holder0 = (ViewHolder0) holder;
            Glide.with(mContext)
                    .load(mFiles.get(position))
                    .into(holder0.mGvImgImg);

            holder0.mGvImgImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnImgDeleteListener.onclick(position);
                }
            });
        } else {
            ViewHolder1 holder1 = (ViewHolder1) holder;
            if (mFiles.size() == 6) {
                holder1.itemView.setVisibility(View.GONE);
            } else {
                holder1.itemView.setVisibility(View.VISIBLE);
                holder1.mAddImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onClick();
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mFiles.size() == 3) {
            return 3;
        } else {
            return mFiles.size() + 1;
        }
    }

    public void setOnImgDeleteListener(OnImgDeleteListener onImgDeleteListener) {
        mOnImgDeleteListener = onImgDeleteListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnImgDeleteListener {
        void onclick(int position);
    }

    public interface OnItemClickListener {
        void onClick();
    }

    static class ViewHolder0 extends RecyclerView.ViewHolder {
        @Bind(R.id.gv_img_img)
        ImageView mGvImgImg;

        ViewHolder0(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder1 extends RecyclerView.ViewHolder {
        @Bind(R.id.add_image)
        RelativeLayout mAddImage;

        ViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
